<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item>
                <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
                <el-button icon="refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="danger" plain icon="delete" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" icon="Position" @click="handleAdd">发起流程</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="edit" :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col>
            <right-toolbar :showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true"/>
            <el-table-column label="任务编码" align="center" prop="id" :show-overflow-tooltip="true"
                v-if="columns.showColumn('id')">
                <template #default="scope">
                    {{ scope.row.id }}
                </template>
            </el-table-column>
            <el-table-column label="关联的工作流ID" align="center" prop="workflowId" :show-overflow-tooltip="true"
                v-if="columns.showColumn('workflowId')">
                <template #default="scope">
                    {{ scope.row.workflowId }}
                </template>
            </el-table-column>
            <el-table-column label="对应的步骤ID" align="center" prop="stepId" :show-overflow-tooltip="true"
                v-if="columns.showColumn('stepId')">
                <template #default="scope">
                    {{ scope.row.stepId }}
                </template>
            </el-table-column>
            <el-table-column label="任务当前状态" align="center" prop="status" :show-overflow-tooltip="true"
                v-if="columns.showColumn('status')">
                <template #default="scope">
                    {{ scope.row.status }}
                </template>
            </el-table-column>
            <el-table-column label="被指派的用户ID" align="center" prop="assignedUserId" :show-overflow-tooltip="true"
                v-if="columns.showColumn('assignedUserId')">
                <template #default="scope">
                    {{ scope.row.assignedUserId }}
                </template>
            </el-table-column>
            <el-table-column label="任务备注信息" align="center" prop="comment" :show-overflow-tooltip="true"
                v-if="columns.showColumn('comment')">
                <template #default="scope">
                    {{ scope.row.comment }}
                </template>
            </el-table-column>
            <el-table-column label="任务创建时间" align="center" prop="createdAt" :show-overflow-tooltip="true"
                v-if="columns.showColumn('createdAt')">
                <template #default="scope">
                    {{ scope.row.createdAt }}
                </template>
            </el-table-column>
            <el-table-column label="任务完成时间" align="center" prop="completedTime" :show-overflow-tooltip="true"
                v-if="columns.showColumn('completedTime')">
                <template #default="scope">
                    {{ scope.row.completedTime }}
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="primary" text icon="delete" @click="handleDelete(scope.row)">删除</el-button>
                    <el-button type="primary" text icon="edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="primary" text icon="Stamp" @click="handleView(scope.row)">去处理</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageIndex"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body draggable align-center>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
                <el-row :gutter="20">
                    <el-col :lg="24">
                        <el-form-item label="任务编码" prop="id">
                            <el-input v-model="form.id" placeholder="请输入任务编码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="关联的工作流ID" prop="workflowId">
                            <el-input v-model="form.workflowId" placeholder="请输入关联的工作流ID" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="对应的步骤ID" prop="stepId">
                            <el-input v-model="form.stepId" placeholder="请输入对应的步骤ID" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="任务当前状态" prop="status">
                            <el-input v-model="form.status" placeholder="请输入任务当前状态" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="被指派的用户ID" prop="assignedUserId">
                            <el-input v-model="form.assignedUserId" placeholder="请输入被指派的用户ID" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="任务备注信息" prop="comment">
                            <el-input v-model="form.comment" placeholder="请输入任务备注信息" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="任务创建时间" prop="createdAt">
                            <el-input v-model="form.createdAt" placeholder="请输入任务创建时间" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="任务完成时间" prop="completedTime">
                            <el-input v-model="form.completedTime" placeholder="请输入任务完成时间" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button type="primary" @click="submitForm" v-if="operateType == 0 || operateType == 1">确
                    定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </template>
        </el-dialog>

        <WorkflowTemplate ref="wftRef" @confirm="onFlowConfirm" />

    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { listWorkflowTask, getWorkflowTask, delWorkflowTask, addWorkflowTask, updateWorkflowTask } from "@/api/workflow/workflowtask.js";
import WorkflowTemplate from './WorkflowTemplate.vue'
const { proxy } = getCurrentInstance()


//弹窗操作类型
//0:新增，1：修改，2:查看
const operateType = ref(0)
//表单ref
const formRef = ref();
const wftRef = ref();
// 遮罩层
const loading = ref(true);
// 选中数组
const selected = ref([]);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 数据集
const dataList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

//默认时间范围
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

// 查询参数
const queryParams = reactive({
    pageIndex: 1,
    pageSize: 10,
});



const state = reactive({
    single: true,
    multiple: true,
    form: {

    },
    rules: {
        //customerId: [{ required: true, message: "客户id不能为空", trigger: "blur"    , type: "number"  }],
        //name: [{ required: true, message: "商品名称不能为空", trigger: "blur"     }],
    },
    options: {
        // 商品种类 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
        typeOptions: [],
    }
})

const { form, rules, options, single, multiple } = toRefs(state)


//所有列
const columns = ref([
    { visible: true, prop: 'id', label: '任务编码' },
    { visible: true, prop: 'workflowId', label: '关联的工作流ID' },
    { visible: true, prop: 'stepId', label: '对应的步骤ID' },
    { visible: true, prop: 'status', label: '任务当前状态' },
    { visible: true, prop: 'assignedUserId', label: '被指派的用户ID' },
    { visible: true, prop: 'comment', label: '任务备注信息' },
    { visible: true, prop: 'createdAt', label: '任务创建时间' },
    { visible: true, prop: 'completedTime', label: '任务完成时间' }
]);

// 获取列表方法
const getList = () => {
    loading.value = true;
    listWorkflowTask(queryParams).then((response) => {
        dataList.value = response.result;
        total.value = response.totalNum;
        loading.value = false;
    });
};

// 重置表单
const reset = () => {

};

// 搜索按钮操作
const handleQuery = () => {
    queryParams.pageIndex = 1;
    getList();
};

// 重置按钮操作
const resetQuery = () => {
    reset();
    handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
    selected.value = selection.map((item) => item.id);
    single.value = selection.length !== 1;
    multiple.value = !selection.length;
};

// 新增按钮操作
const handleAdd = () => {
    // reset();
    // operateType.value = 0;
    // open.value = true;
    // title.value = "添加数据";
    wftRef.value.show();
};

// 修改按钮操作
const handleUpdate = (row) => {
    reset();
    operateType.value = 1;
    const key = row.id || selected.value;
    getWorkflowTask(key).then((response) => {
        Object.assign(form.value, response);
        open.value = true;
        title.value = "修改数据";
    });
};


//查看
const handleView = (row) => {
    reset();
    operateType.value = 2;
    var key = row.id;
    getWorkflowTask(key).then((response) => {
        Object.assign(form.value, response);
        open.value = true;
        title.value = "查看明细";
    });
}

// 提交按钮
const submitForm = () => {
    const validateForm = formRef.value?.validate;
    if (validateForm) {
        validateForm((valid) => {
            if (valid) {
                const apiCall = form.value.id ? updateWorkflowTask : addWorkflowTask;
                apiCall(form.value).then(() => {
                    proxy.$modal.msgSuccess("操作成功");
                    open.value = false;
                    getList();
                });
            }
        });
    }
};

//取消弹窗
const cancel = () => {
    open.value = false;
    proxy.resetForm("form")
}

// 删除按钮操作
const handleDelete = (row) => {
    const keys = [].concat(row.id || selected.value);
    proxy.$modal.confirm("是否确认删除编号为" + keys + "的数据项？")
        .then(() => delWorkflowTask(keys))
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
};

//确认选择流程模板
const onFlowConfirm = (templateId) => {
    addWorkflowTask({}).then(res => {
        console.log(res);
    });
    console.log(templateId)
}

// 导出按钮操作
const handleExport = () => {
    download("system//export", queryParams, `menu_${new Date().getTime()}.xlsx`);
};

// 页面加载时调用获取列表方法
onMounted(getList);
</script>
